package com.jinmdz.fmis.api.service;

import com.jinmdz.fmis.api.base.BaseService;
import com.jinmdz.fmis.api.constant.FileConst;
import com.jinmdz.fmis.api.model.config.ConfigApp;
import com.jinmdz.fmis.api.model.system.UserItem;
import com.jinmdz.fmis.api.model.test.BusinessData;
import com.jinmdz.fmis.api.model.test.ExportData;
import com.jinmdz.fmis.api.model.test.TextInfo;
import com.jinmdz.fmis.api.model.test.ValidateData;
import com.jinmdz.fmis.api.wrapper.DocumentWrapper;
import com.jinmdz.fmis.api.wrapper.ResponseWrapper;
import com.jinmdz.fmis.api.wrapper.ToolWrapper;
import com.jinmdz.fmis.common.model.PoiTable;
import com.jinmdz.fmis.common.util.BeanUtil;
import com.jinmdz.fmis.common.util.DataUtil;
import com.jinmdz.fmis.common.util.DateUtil;
import com.jinmdz.fmis.common.util.third.PoiUtil;
import com.jinmdz.fmis.common.wrapper.PoiWrapper;
import com.jinmdz.fmis.core.base.BaseResult;
import com.jinmdz.fmis.core.constant.EResultCode;
import com.jinmdz.fmis.core.wrapper.ResultWrapper;
import com.jinmdz.fmis.dao.model.document.DocumentFileItem;
import com.jinmdz.fmis.mapper.entity.DocumentFileEntity;
import com.jinmdz.fmis.mapper.mapper.DocumentFileMapper;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;

/**
 * test测试Service类
 *
 * @author LiCongLu
 * @date 2020-01-06 14:17
 */
@Service("testService")
public class TestService extends BaseService {

    @Resource
    private ConfigApp configApp;

    @Resource
    private ResponseWrapper responseWrapper;

    @Resource
    private DocumentWrapper documentWrapper;

    @Resource
    private ToolWrapper toolWrapper;

    @Resource
    private DocumentFileMapper documentFileMapper;

    /**
     * 测试导出表格
     *
     * @param data     测试请求数据
     * @param response 响应
     * @return
     * @author LiCongLu
     * @date 2020-01-03 14:20
     */
    public void exportExcelFile(ExportData data, HttpServletResponse response) {
        ArrayList<TextInfo> dataList = new ArrayList<>();

        // 获取当前时间
        Date nowDate = nowDate();

        int rowNum = data.getRowNum() != null && data.getRowNum().intValue() > 0 ? data.getRowNum() : 100;
        for (int i = 0; i < rowNum; i++) {
            dataList.add(getTextInfo(i, nowDate));
        }
        PoiTable poiTable = PoiWrapper.createPoiTable(TextInfo.class, dataList);

        if (poiTable.getTitleCell() != null) {
            if (data.getTitle() != null && data.getTitle().length() > 0) {
                poiTable.getTitleCell().setCellValue(data.getTitle());
            }
        }

        XSSFWorkbook workbook = PoiUtil.createXSSFWorkbook(poiTable);

        String filename = DateUtil.formatPattern(nowDate(), "yyyy_MM_dd_HH_mm_ss");
        responseWrapper.downloadXlsx(response, workbook, filename);
    }

    private TextInfo getTextInfo(int i, Date nowDate) {
        TextInfo info = new TextInfo();
        info.setUsername("测试" + i);
        info.setSex(i % 2 == 1 ? "男" : "女");
        info.setAge(i);
        info.setIdCard("110101199003073239" + i);
        info.setBirthday(nowDate);
        info.setTelephone("15253653523" + i);
        info.setMoney(new BigDecimal(23234234L));
        info.setNumber(2323.3634D);
        info.setCharge(3234.4546F);
        return info;
    }


    /**
     * 上传图片文件
     *
     * @param data      请求数据
     * @param imageFile 图片文件
     * @return
     * @author LiCongLu
     * @date 2020-01-06 14:41
     */
    public BaseResult uploadImageFile(BusinessData data, MultipartFile imageFile) {
        DocumentFileEntity entity = documentWrapper.saveImageFile(imageFile);
        if (entity != null) {
            entity.setTableName(toolWrapper.getTableName(DocumentFileEntity.class))
                    .setTableId(0)
                    .setBusinessCode(data.getBusinessCode())
                    .setDocumentTypeCode(0)
                    .setFileCode(FileConst.FileCode.Image_QRCode)
                    .setRandomCode(DataUtil.getUUID());

            entity.setFuneralParlourCode(configApp.getFuneralParlourCode());
            entity.setCreateUserId(0)
                    .setModifiedUserId(0);

            documentFileMapper.insertDocumentFile(entity);

            if (entity.getId() != null && entity.getId().intValue() > 0) {
                DocumentFileItem item = BeanUtil.copy2Bean(entity, new DocumentFileItem());
                BaseResult<DocumentFileItem> result = ResultWrapper.getBaseResult();
                return result.setData(item).setResultCode(EResultCode.CODE1).setMessage("上传成功");
            } else {
                return failure("上传失败");
            }
        }

        return ResultWrapper.getResult();
    }

    /**
     * 生成二维码图片文件
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-01-07 11:38
     */
    public BaseResult loadQRCodeFile(BusinessData data) {
        if (data.getBusinessCode() == null || data.getBusinessCode().length() == 0) {
            return ResultWrapper.failure("业务编码不存在！");
        }

        DocumentFileEntity entity = documentWrapper.saveQRCodeImage(data.getBusinessCode());
        if (entity != null) {
            entity.setTableName(toolWrapper.getTableName(DocumentFileEntity.class))
                    .setTableId(0)
                    .setBusinessCode(data.getBusinessCode())
                    .setDocumentTypeCode(0)
                    .setFileCode(FileConst.FileCode.Image)
                    .setRandomCode(DataUtil.getUUID());

            entity.setFuneralParlourCode(configApp.getFuneralParlourCode());
            entity.setCreateUserId(0)
                    .setModifiedUserId(0);

            documentFileMapper.insertDocumentFile(entity);

            if (entity.getId() != null && entity.getId().intValue() > 0) {
                DocumentFileItem item = BeanUtil.copy2Bean(entity, new DocumentFileItem());
                BaseResult<DocumentFileItem> result = ResultWrapper.getBaseResult();
                return result.setData(item).setResultCode(EResultCode.CODE1).setMessage("生成成功");
            } else {
                return failure("生成失败");
            }
        }

        return ResultWrapper.getResult();
    }

    /**
     * 验证请求数据
     *
     * @param data     请求数据
     * @param userItem 当前帐号
     * @return
     * @author LiCongLu
     * @date 2020-01-09 15:18
     */
    public BaseResult validateData(ValidateData data, UserItem userItem) {

        return ResultWrapper.getResult();
    }
}
